<?php
if ( isset($_POST['submitted']) ) {
	check_admin_referer('fts-urlshortener');
	$options = array();

	//main values
	$options['urlserviceenable'] = $_POST['urlserviceenable']; 
	$options['urlservice '] = $_POST['urlservice'];
	$options['urlautogen '] = $_POST['urlautogen'];
	$options['ownservice'] = $_POST['ownservice'];
	$options['urlbetaservices'] = $_POST['urlbetaservices'];
	
	//Service Config
	$options['apiuser_supr'] = $_POST['apiuser_supr'];
	$options['apikey_supr'] = $_POST['apikey_supr'];
	$options['apiuser_bitly'] = $_POST['apiuser_bitly'];
	$options['apikey_bitly '] = $_POST['apikey_bitly'];
	$options['apiuser_trim'] = $_POST['apiuser_trim'];
	$options['apikey_trim'] = $_POST['apikey_trim'];
	$options['apiuser_snip'] = $_POST['apiuser_snip'];
	$options['apikey_snip '] = $_POST['apikey_snip'];
	$options['snipprefix'] = $_POST['snipprefix'];
	$options['apikey_cligs'] = $_POST['apikey_cligs'];
	$options['apiuser_shortie'] = $_POST['apiuser_shortie'];
	$options['apikey_shortie'] = $_POST['apikey_shortie'];
	$options['apikey_pingfm'] = $_POST['apikey_pingfm'];
	$options['apikey_awesm'] = $_POST['apikey_awesm'];
	
	//POST ID Short
	$options['ownservicetype'] = $_POST['ownservicetype'];
	$options['ownserviceprefix'] = $_POST['ownserviceprefix'];

	//tweet
	$options['tweet'] = $_POST['tweet'];
	$options['tweet_user'] = $_POST['tweet_user'];
	$options['tweet_pass'] = $_POST['tweet_pass'];


	update_option($this->db_option, $options);
	echo '<div class="updated fade"><p>Plugin settings saved.</p></div>';
}

$options = $this->my_options();

//main values
$urlserviceenable = $options['urlserviceenable'];
$urlservice = $options['urlservice'];
$urlautogen = $options['urlautogen'];
$ownservice = $options['ownservice'];
$urlbetaservices = $options['urlbetaservices'];

//Service Config
$apiuser_supr = $options['apiuser_supr'];
$apikey_supr = $options['apikey_supr'];
$apiuser_bitly = $options['apiuser_bitly'];
$apikey_bitly = $options['apikey_bitly'];
$apiuser_trim = $options['apiuser_trim'];
$apikey_trim = $options['apikey_trim'];
$apiuser_snip = $options['apiuser_snip'];
$apikey_snip = $options['apikey_snip'];
$snipprefix = $options['snipprefix'];
$apikey_cligs = $options['apikey_cligs'];
$apiuser_shortie = $options['apiuser_shortie'];
$apikey_shortie = $options['apikey_shortie'];
$apikey_pingfm = $options['apikey_pingfm'];
$apikey_awesm = $options['apikey_awesm'];

//POST ID Short
$ownservicetype = $options['ownservicetype'];
$ownserviceprefix = $options['ownserviceprefix'];

//tweet
$tweet = $options['tweet'];
$tweet_user = $options['tweet_user'];
$tweet_pass = $options['tweet_pass'];

//others
$module_urls[0] = "http://fusedthought.com/downloads/addon-module-for-url-shortener-wordpress-plugin/";
?>

<div class="wrap">
<!--start wrap-->
	<div id="plugin-des">
		<h2>URL Shortener<span class="pluginbyline">by <a href="http://fusedthought.com">Gerald Yeo</a> (Fusedthought.com)</span></h2>
		<div class="logo">
			<a href="http://fusedthought.com/downloads/url-shortener-wordpress-plugin/"><img src="<?php echo $this->plugin_url; ?>/plugin-logo.jpg" alt="" /></a>
		</div>
		<p>This plugin allows you to :
			<ul class="ptshow">
				<li>Create shorter URLs using WordPress Post IDs, redirecting <em>http://yoursite/POST-ID</em> to the actual post.</li>
				<li>Generate Short URLs using 3rd Party Services</li>
				<li>Publish (Post Title and URL) to Twitter</li>
			</ul>
		</p>
		<p>It bring's the one-click/automated URL Shortening functionality to your WordPress.org installation.</p>
	</div>
	
	<form method="post" action="<?php echo $action_url ?>">
		<?php wp_nonce_field('fts-urlshortener'); ?>
		<input type="hidden" name="submitted" value="1" /> 
		
		<h4>Integration Options</h4>
		<div class="main-option">
		<!--start main-->
			<label class="main-label">Enable Shortening Service Integration:</label>
			<select name="urlserviceenable" class="main-select">
				<option value="yes" <?php selected('yes', $urlserviceenable); ?>>Yes &nbsp;</option>
				<option value="no" <?php selected('no', $urlserviceenable); ?>>No &nbsp;</option>
			</select>	
			<div class="sub-option <?php echo ($urlserviceenable != 'yes' ? 'hidden' : '')?>">
			<!--start sub-->
				<fieldset title="URL Shortening Services">
					<label for="shortener_services">You're using:</label>	
					<select name="urlservice" id="shortener_services">
						<option value="none" <?php selected( 'none', $urlservice); ?>>Select Service... &nbsp;</option>
						<option value="tinyurl" <?php selected( 'tinyurl',$urlservice); ?>> TinyURL &nbsp;</option>				
						<option value="supr" <?php selected( 'supr', $urlservice); ?>>Su.pr (by StumbleUpon) &nbsp;</option>
						<option value="isgd" <?php selected( 'isgd', $urlservice); ?>>is.gd &nbsp;</option>
						<option value="bitly" <?php selected( 'bitly', $urlservice); ?>>bit.ly &nbsp;</option>
						<option value="trim" <?php selected( 'trim', $urlservice); ?>>tr.im &nbsp;</option>
						<option value="snipurl" <?php selected( 'snipurl', $urlservice); ?>>Snipr / Snipurl / Snurl &nbsp;</option>
						<option value="cligs" <?php selected( 'cligs', $urlservice); ?>>Cligs (Cl.gs &nbsp;)</option>
						<option value="shortie" <?php selected( 'shortie', $urlservice); ?>>Short.ie &nbsp;</option>
						<option value="shortto" <?php selected( 'shortto', $urlservice); ?>>Short.to &nbsp;</option>
						<option value="chilpit" <?php selected( 'chilpit', $urlservice); ?>>Chilp.it &nbsp;</option>
						<option value="pingfm" <?php selected( 'pingfm', $urlservice); ?>>Ping.fm &nbsp;</option>
						<option value="smsh" <?php selected( 'smsh', $urlservice); ?>>sm00sh / smsh &nbsp;</option>
						<option value="unu" <?php selected( 'unu', $urlservice); ?>>u.nu &nbsp;</option>
						<option value="unfakeit" <?php selected( 'unfakeit',$urlservice); ?>>unfake.it &nbsp;</option>
						<option value="awesm" <?php selected( 'awesm',$urlservice); ?>>awe.sm &nbsp;</option>
					</select>
					
					<div id="userkey_supr" class="APIConfig <?php echo($urlservice != 'supr'? 'hidden' : 'show');?>">
						<label for="apiuser_supr">API User (Optional)</label> 
						<input type="text" name="apiuser_supr" value="<?php echo $apiuser_supr; ?>" />
						<label for="apikey_supr">API Key (Optional)</label> 
						<input type="text" name="apikey_supr" value="<?php echo $apikey_supr; ?>" />	
					</div>
					<div id="userkey_bitly" class="APIConfig <?php echo($urlservice != 'bitly'? 'hidden' : 'show');?>">
						<label for="apiuser_bitly">API User (Required)</label> 
						<input type="text" name="apiuser_bitly" value="<?php echo $apiuser_bitly; ?>" />
						<label for="apikey_bitly">API Key (Required)</label> 
						<input type="text" name="apikey_bitly" value="<?php echo $apikey_bitly; ?>" />	
					</div>
					<div id="userkey_trim" class="APIConfig <?php echo($urlservice != 'trim'? 'hidden' : 'show');?>">
						<label for="apiuser_trim">API User (Required)</label> 
						<input type="text" name="apiuser_trim" value="<?php echo $apiuser_trim; ?>" />
						<label for="apikey_trim">API Key (Required)</label> 
						<input type="text" name="apikey_trim" value="<?php echo $apikey_trim; ?>" />	
					</div>
					<div id="userkey_snipurl" class="APIConfig <?php echo($urlservice != 'snipurl'? 'hidden' : 'show');?>">
						<label for="apiuser_snipurl">API User (Required)</label> 
						<input type="text" name="apiuser_snipurl" value="<?php echo $apiuser_snipurl; ?>" />
						<label for="apikey_snipurl">API Key (Required)</label> 
						<input type="text" name="apikey_snipurl" value="<?php echo $apikey_snipurl; ?>" />
						<label for="snipprefix">Select Prefix (Default: Snipr.com)</label>
						<select name="snipprefix" id="snipprefixsel" >
							<option value="snipr" <?php selected('snipr', $snipprefix); ?>>Snipr.com &nbsp;</option>												
							<option value="snurl" <?php selected('snurl', $snipprefix); ?>>Snurl.com &nbsp;</option>
							<option value="snipurl" <?php selected('snipurl', $snipprefix); ?>>Snipurl.com &nbsp;</option>
						</select>	
					</div>
					<div id="userkey_cligs" class="APIConfig <?php echo($urlservice != 'cligs'? 'hidden' : 'show');?>">
						<label for="apikey_cligs">API Key (Optional)</label> 
						<input type="text" name="apikey_cligs" value="<?php echo $apikey_cligs; ?>" />	
					</div>
					<div id="userkey_shortie" class="APIConfig <?php echo($urlservice != 'shortie'? 'hidden' : 'show');?>">
						<label for="apiuser_shortie">API User (Optional)</label> 
						<input type="text" name="apiuser_shortie" value="<?php echo $apiuser_shortie; ?>" />
						<label for="apikey_shortie">API Key (Optional)</label> 
						<input type="text" name="apikey_shortie" value="<?php echo $apikey_shortie; ?>" />	
					</div>
					<div id="userkey_pingfm" class="APIConfig <?php echo($urlservice != 'pingfm'? 'hidden' : 'show');?>">
						<label for="apikey_pingfm">API Key (Required)</label> 
						<input type="text" name="apikey_pingfm" value="<?php echo $apikey_pingfm; ?>" />	
					</div>
					<div id="userkey_awesm" class="APIConfig <?php echo($urlservice != 'awesm'? 'hidden' : 'show');?>"> 
						<label for="apikey_awesm">API Key (Required)</label> 
						<input type="text" name="apikey_awesm" value="<?php echo $apikey_awesm; ?>" />	
					</div>
				</fieldset>
				<fieldset title="URL Auto Generation Option">
					<label for="autogen">Enable Automatic Short URL Generation upon Publishing: </label>
					<select name="urlautogen">
						<option value="yes" <?php selected('yes', $urlautogen); ?>>Yes &nbsp;</option>				
						<option value="no" <?php selected('no', $urlautogen); ?>>No &nbsp;</option>
					</select>
				</fieldset>
				<fieldset title="Twitter Module" id="twitter-module">
					<label for="tweet">Post to Twitter</label>
					<select name="tweet">
						<option value="disable" <?php selected('disable', $tweet); ?>>Disable &nbsp;</option>							
						<option value="manual" <?php selected('manual', $tweet); ?>>Manual &nbsp;</option>
						<option value="auto" <?php selected('auto', $tweet); ?>>Auto &nbsp;</option>				
					</select>
					<div class="<?php echo($tweet == 'manual' || $tweet == 'auto' ? 'show' : 'hidden' ?>">
						<label for="tweet_user">Twitter Username (Required)</label> 
						<input type="text" id="tweet_user" name="tweet_user" value="<?php echo $tweet_user; ?>" />
						<label for="tweet_pass">Twitter Password (Required)</label> 
						<input  type="password" id="tweet_pass" name="tweet_pass" value="<?php echo $tweet_pass; ?>" />
					</div>
					<a class="svcqn" href="#">[?]</a>
					<div class="svcqn-des hidden">
						<p><strong>Disable</strong>: Entire Twitter module will be disabled</p>
						<p><strong>Manual</strong>: Option to post updates to twitter will be made available</p>
						<p><strong>Auto</strong>: Automatically post your Title and the Short URL to Twitter upon post publishing.</p>
						<p>(Format of update: "Title, Short URL")</p>
					</div>
				</fieldset>
				<?php if ($this->is_active('url-shortener-addon-module/fts-shortenurl-addon-module.php')){?>
				<fieldset title="Beta Module" id="beta-module">
					<label for="urlbetaservices">Enable Addon Module (for bonus/beta services): </label>
					<select name="urlbetaservices">
						<option value="no" <?php selected('no', $urlbetaservices); ?>>No &nbsp;</option>							
						<option value="yes" <?php selected('yes', $urlbetaservices); ?>>Yes &nbsp;</option>				
					</select> 
					<a class="svcqn" href="#">[?]</a>
					<p class="svcqn-des hidden">The addon module provides bonus services (highlighted in green) and beta services (highlighted in red) which are not available in the main URL Shortener Plugin.</p>
				</fieldset>
				<?php } //end addon mod
				
				$more_modules = '<div class="fakefield"><p><strong>Additional plugins/modules which can be integrated:</strong></p><ol>';
				if (!$this->is_active('url-shortener-addon-module/fts-shortenurl-addon-module.php')){
					$more_modules .= '<li><a href="'.$module_urls[0].'">URL Shortener Addon Module</a> - Provides bonus or beta URL Shortener Services</li>';
				}
				$more_modules .='<ol></div>';
				if (!$this->is_active('url-shortener-addon-module/fts-shortenurl-addon-module.php')){
					print $more_modules;
				} ?>
			<!--end sub-->
			</div>
		<!--end main-->
		</div>
		<div class="main-option">
		<!--start main-->
			<label class="main-label" for="ownserice">Enable Short URLs using Post ID:</label>
			<select name="ownservice"  class="main-select">
				<option value="yes"<?php selected('yes', $ownservice); ?>>Yes &nbsp;</option>				
				<option value="no"<?php selected('no', $ownservice); ?>>No &nbsp;</option>
			</select>	
			<div class="sub-option <?php echo ($ownservice != 'yes' ? 'hidden' : '')?>">
			<!--start sub-->
				<fieldset title="Post ID Redirection">
					<label for="ownservicetype">Select type of redirect:</label>		
					<select name="ownservicetype">
						<option value="templateredirect"<?php selected('templateredirect', $ownservicetype); ?>>Template Redirect &nbsp;</option>				
						<option value="wprewriteredirect"<?php selected('wprewriteredirect', $ownservicetype); ?>>WP_Rewrite Redirect &nbsp;</option>
					</select>
					<div id="template-des" class="<?php echo($ownservicetype == 'templateredirect' ? 'show' : 'hidden');?>">
						<p>If template redirect is chosen, you can add a prefix when displaying the URLs without prior configuration. The redirection function will take that into account.</p>
						<p>Appending of Prefixes are supported.</p>
						<small>Eg: http://yoursite/<em>prefix/</em>post-id or http://yoursite/<em>prefix</em>post-id</small>
					</div>
					<div id="wprewrite-des" class="<?php echo($ownservicetype == 'wprewriteredirect' ? 'show' : 'hidden');?>">
						<p>NOTE: <strong>WP_Rewrite Redirect</strong> has <em>NO</em> Page redirect support. It will <em>ONLY</em> redirect posts.</p>
						<p>I would recommend using the "template redirect" method as it does a 301 (Permanent) Redirection, and hence more search engine friendly.</p>
						<label for="ownserviceprefix">If WP_Rewrite Redirect is selected, please input your preferred prefix (optional):</label>
						<input type="text" name="ownserviceprefix" value="<?php echo $ownserviceprefix; ?>" />
						<small>For example:</small>
						<small>If "prefix/" was set: URL would be http://yoursite/<em>prefix/</em>post-id </small>
						<small>If "prefix" (without "/") was set: URL would be http://yoursite/<em>prefix</em>post-id</small>

					</div>
				</fieldset>
			<!--end sub-->
			</div>
		<!--end main-->
		</div>		
		<h4>Save Your Settings</h4>
		<div class="reqfielderror"></div>
		<p class="submit"><input type="submit" id="submit-button" class="button-primary" value="<?php _e('Save Changes') ?>" /></p>	
	</form>
<!--end wrap-->
</div>